from pydantic import BaseModel
from typing import Optional
from datetime import datetime
from enum import Enum

# 用户角色枚举
class UserRole(str, Enum):
    """用户角色枚举"""
    USER = "user"      # 普通用户
    ADMIN = "admin"    # 管理员

# 用户相关模型
class UserBase(BaseModel):
    """用户基础模型"""
    username: str

class UserCreate(UserBase):
    """创建用户的请求模型"""
    password: str

class UserLogin(BaseModel):
    """用户登录请求模型"""
    username: str
    password: str

class User(UserBase):
    """完整的用户模型"""
    id: int
    role: UserRole = UserRole.USER  # 添加角色字段
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

class Token(BaseModel):
    """令牌响应模型"""
    access_token: str
    token_type: str
    user: User

class LoginResponse(BaseModel):
    """登录响应模型"""
    success: bool
    message: str
    data: Optional[Token] = None

class TodoStatus(str, Enum):
    """待办事项状态枚举"""
    PENDING = "pending"
    IN_PROGRESS = "in_progress" 
    COMPLETED = "completed"

class TodoPriority(str, Enum):
    """待办事项优先级枚举"""
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"

class TodoBase(BaseModel):
    """待办事项基础模型"""
    title: str
    description: Optional[str] = None
    status: TodoStatus = TodoStatus.PENDING
    priority: TodoPriority = TodoPriority.MEDIUM
    order: int = 0  # 排序字段，数值越小越靠前
    assigned_by_admin: bool = False  # 标识任务是否由管理员分配
    
class TodoCreate(TodoBase):
    """创建待办事项的请求模型"""
    pass

class TodoUpdate(BaseModel):
    """更新待办事项的请求模型"""
    title: Optional[str] = None
    description: Optional[str] = None
    status: Optional[TodoStatus] = None
    priority: Optional[TodoPriority] = None
    order: Optional[int] = None  # 支持更新排序

class Todo(TodoBase):
    """完整的待办事项模型"""
    id: int
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

class TodoResponse(BaseModel):
    """API响应模型"""
    success: bool
    message: str
    data: Optional[Todo] = None

class TodoListResponse(BaseModel):
    """待办事项列表响应模型"""
    success: bool
    message: str
    data: list[Todo]
    total: int

# 管理员相关模型
class UserListResponse(BaseModel):
    """用户列表响应模型"""
    success: bool
    message: str
    data: list[User]
    total: int

class AdminTodoCreate(BaseModel):
    """管理员为用户创建任务的请求模型"""
    title: str
    description: Optional[str] = None
    status: TodoStatus = TodoStatus.PENDING
    priority: TodoPriority = TodoPriority.MEDIUM
    assigned_to_user_id: int  # 分配给的用户ID

class AdminTodoAssign(BaseModel):
    """管理员分配任务的请求模型"""
    todo_id: int
    assigned_to_user_id: int

# 通知相关模型
class NotificationBase(BaseModel):
    """通知基础模型"""
    title: str
    message: str

class NotificationCreate(NotificationBase):
    """创建通知的请求模型"""
    user_id: int

class Notification(NotificationBase):
    """完整的通知模型"""
    id: int
    user_id: int
    is_read: bool = False
    created_at: datetime
    updated_at: datetime
    
    class Config:
        from_attributes = True

class NotificationResponse(BaseModel):
    """通知API响应模型"""
    success: bool
    message: str
    data: Optional[Notification] = None

class NotificationListResponse(BaseModel):
    """通知列表响应模型"""
    success: bool
    message: str
    data: list[Notification]
    total: int
    unread_count: int